package miiteen.sys.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
public class SqlSessionFactoryAutoConfig {

    @Bean(value = "datasource1")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("datasource1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setConfigLocation((resolver.getResource("classpath:mybatis-config.xml")));
        sqlSessionFactoryBean.setMapperLocations((resolver.getResources("classpath*:miiteen/**/**/**/*.xml")));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(value = "datasource2")
    @ConfigurationProperties(prefix = "datasource2")
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory2")
    public SqlSessionFactory sqlSessionFactory2(@Qualifier("datasource2") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatis-config-ce.xml"));
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/miiteen/demo/web/**/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(value = "datasource3")
    @ConfigurationProperties(prefix = "datasource3")
    public DataSource dataSource3() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory3")
    public SqlSessionFactory sqlSessionFactory3(@Qualifier("datasource3") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatis-config-du.xml"));
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/miiteen/demo/duty/**/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(value = "datasource4")
    @ConfigurationProperties(prefix = "datasource4")
    public DataSource dataSource4() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory4")
    public SqlSessionFactory sqlSessionFactory4(@Qualifier("datasource4") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatis-config-lo.xml"));
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/miiteen/demo/authentication/**/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(value = "datasource5")
    @ConfigurationProperties(prefix = "datasource5")
    public DataSource dataSource5() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory5")
    public SqlSessionFactory sqlSessionFactory5(@Qualifier("datasource5") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatis-config-ce.xml"));
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/miiteen/demo/web/**/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(value = "hbdatasource")
    @ConfigurationProperties(prefix = "hbdatasource")
    public DataSource hbDatasource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "hbSessionFactory")
    public SqlSessionFactory hbSessionFactory(@Qualifier("hbdatasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatis-config-hb.xml"));
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/miiteen/hb/**/**/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }
}